package com.bcu.cn.onlineexamsystem.homemanage.controller;

import com.bcu.cn.onlineexamsystem.user.entity.User;
import com.bcu.cn.onlineexamsystem.studentmanage.controller.StudentHomeDTO;
import com.bcu.cn.onlineexamsystem.studentmanage.service.StudentHomeService;
import com.bcu.cn.onlineexamsystem.teacherhomemanage.controller.TeacherHomeDTO;
import com.bcu.cn.onlineexamsystem.teacherhomemanage.service.TeacherHomeService;
import com.bcu.cn.onlineexamsystem.adminmanage.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import jakarta.servlet.http.HttpSession;

@Controller
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private StudentHomeService studentHomeService;
    
    @Autowired
    private TeacherHomeService teacherHomeService;

    @Autowired
    private AdminUserService adminUserService;

    @GetMapping("/manage")
    public String home(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        model.addAttribute("user", user);
        switch (user.getRole()) {
            case "student":
                // 为学生加载首页数据
                StudentHomeDTO homeData = studentHomeService.getHomeData(user.getUserId());
                model.addAttribute("homeData", homeData);
                return "student/home";
            case "teacher":
                // 为教师加载首页数据
                TeacherHomeDTO teacherHomeData = teacherHomeService.getHomeData(user.getUserId());
                model.addAttribute("homeData", teacherHomeData);
                return "teacher/home";
            case "admin":
                model.addAttribute("users", adminUserService.getAllUsers());
                model.addAttribute("classes", adminUserService.getAllClasses());
                return "admin/home";
            default:
                return "redirect:/login";
        }
    }
}